package com.xzy.redis.controller;

import com.xzy.redis.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redisTest")
public class redisController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private User user;
    public static void initUser(User user){
        user=new User(1,"xzy","123456");
    }
    @GetMapping
    public String redisTest(){
//        设置值到redis
        redisTemplate.opsForValue().set("name","xzy");
//        从redis中获取
        Object name = redisTemplate.opsForValue().get("name");
        return (String) name;
    }
    @GetMapping("/user")
    public String userRedis(){
        user=new User(1,"xzy","123456");
        System.out.println(user.toString());
        redisTemplate.opsForValue().set("user1",user);
        Object user = redisTemplate.opsForValue().get("user1");
        return user.toString();
    }
}
